{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c1271f63-22c4-4151-83ae-0ea36be59c17",
   "metadata": {},
   "source": [
    "## The Magic Commands in `Chapyter`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "305f5dfc-d2e5-44eb-b6b5-4850e86274f4",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "%load_ext chapyter\n",
    "# You might want to create an `.env` file in the current\n",
    "# directory that contains\n",
    "# OPENAI_API_KEY=sk-xxxx\n",
    "# OPENAI_ORGANIZATION=org-xxxx\n",
    "# For other ways for configurations, including how to use Azure OpenAI APIs, \n",
    "# you can check the 02-configure-chapyter.ipynb notebook for more details."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4caec9f-71d7-4674-9e4c-29279981fed2",
   "metadata": {},
   "source": [
    "PS: You will see neat UIs if you download and execute the following cells one by one in jupyterlab. \n",
    "![chapyter-starter.png](https://www.szj.io/assets/files/data/chapyter-starter.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb8888ac-5bec-4c62-9de6-21d45ce89ff1",
   "metadata": {},
   "source": [
    "### `%%chat`: start coding in natural language"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "effc4a91-fc5e-42c9-b576-e0c37ecd4478",
   "metadata": {
    "ChapyterCell": {
     "cellType": "original",
     "linkedCellId": "93c33a3e-710a-4472-922c-1458d98c32d0"
    }
   },
   "outputs": [],
   "source": [
    "%%chat \n",
    "list all the files in the current directory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "93c33a3e-710a-4472-922c-1458d98c32d0",
   "metadata": {
    "ChapyterCell": {
     "cellType": "generated",
     "linkedCellId": "effc4a91-fc5e-42c9-b576-e0c37ecd4478"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['test', '.env', 'starter-Copy2.ipynb', '.ipynb_checkpoints', 'starter.ipynb', 'starter-Copy1.ipynb']\n"
     ]
    }
   ],
   "source": [
    "# Assistant Code for Cell [2]:\n",
    "# Sure, here is the Python code to list all the files in the current directory:\n",
    "import os\n",
    "\n",
    "files = os.listdir('.')\n",
    "print(files)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "29533815-5d55-4281-8446-f2e1b810b040",
   "metadata": {
    "ChapyterCell": {
     "cellType": "original",
     "linkedCellId": "84daa58f-c55d-4b75-8540-47eb6d0441df"
    }
   },
   "outputs": [],
   "source": [
    "%%chat \n",
    "\n",
    "find the files in the current directory that has the longest file name "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "84daa58f-c55d-4b75-8540-47eb6d0441df",
   "metadata": {
    "ChapyterCell": {
     "cellType": "generated",
     "linkedCellId": "29533815-5d55-4281-8446-f2e1b810b040"
    },
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "starter-Copy2.ipynb\n"
     ]
    }
   ],
   "source": [
    "# Assistant Code for Cell [3]:\n",
    "# Sure, here is the Python code to find the file with the longest name in the current directory.\n",
    "import os\n",
    "\n",
    "# Get all files in current directory\n",
    "files = os.listdir('.')\n",
    "\n",
    "# Find the file with the longest name\n",
    "longest_file = max(files, key=len)\n",
    "\n",
    "print(longest_file)\n",
    "# This code will print the name of the file with the longest name in the current directory."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7f055c4f-f714-4620-9e1e-d629ab8a4d87",
   "metadata": {
    "ChapyterCell": {
     "cellType": "original",
     "linkedCellId": "c2927cf9-766e-4c03-b039-5d6ece37d958"
    }
   },
   "outputs": [],
   "source": [
    "%%chat \n",
    "\n",
    "Generate a dictionary that groups the files in the current folder based on their types"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c2927cf9-766e-4c03-b039-5d6ece37d958",
   "metadata": {
    "ChapyterCell": {
     "cellType": "generated",
     "linkedCellId": "7f055c4f-f714-4620-9e1e-d629ab8a4d87"
    },
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'': ['test', '.env', '.ipynb_checkpoints'], '.ipynb': ['starter-Copy2.ipynb', 'starter.ipynb', 'starter-Copy1.ipynb']}\n"
     ]
    }
   ],
   "source": [
    "# Assistant Code for Cell [4]:\n",
    "# Sure, here is a Python script that does that:\n",
    "import os\n",
    "\n",
    "# Get all files in current directory\n",
    "files = os.listdir('.')\n",
    "\n",
    "# Initialize an empty dictionary to store the file types and names\n",
    "file_dict = {}\n",
    "\n",
    "# Loop through each file\n",
    "for file in files:\n",
    "    # Get the file extension\n",
    "    file_type = os.path.splitext(file)[1]\n",
    "    \n",
    "    # If the file type is not in the dictionary, add it\n",
    "    if file_type not in file_dict:\n",
    "        file_dict[file_type] = []\n",
    "    \n",
    "    # Add the file name to the list of files for this type\n",
    "    file_dict[file_type].append(file)\n",
    "\n",
    "print(file_dict)\n",
    "# This script will print a dictionary where the keys are file extensions (like '.txt', '.py', etc.) and the values are lists of file names with that extension."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a696eca-5a97-4138-9acc-518db632b5c5",
   "metadata": {},
   "source": [
    "### `%%chat -h` will add execution history into the prompt "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "57e0a283-95d9-410a-bcdb-9efde7d252b2",
   "metadata": {
    "ChapyterCell": {
     "cellType": "original",
     "linkedCellId": "2285fa4c-14ed-4d7f-861f-aba5c89d1daa"
    }
   },
   "outputs": [],
   "source": [
    "%%chat -h \n",
    "\n",
    "Define functions for the code above:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2285fa4c-14ed-4d7f-861f-aba5c89d1daa",
   "metadata": {
    "ChapyterCell": {
     "cellType": "generated",
     "linkedCellId": "57e0a283-95d9-410a-bcdb-9efde7d252b2"
    },
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['test', '.env', 'starter-Copy2.ipynb', '.ipynb_checkpoints', 'starter.ipynb', 'starter-Copy1.ipynb']\n",
      "starter-Copy2.ipynb\n",
      "{'': ['test', '.env', '.ipynb_checkpoints'], '.ipynb': ['starter-Copy2.ipynb', 'starter.ipynb', 'starter-Copy1.ipynb']}\n"
     ]
    }
   ],
   "source": [
    "# Assistant Code for Cell [8]:\n",
    "# Sure, here is how you can define functions for the code above:\n",
    "import os\n",
    "\n",
    "def list_files():\n",
    "    files = os.listdir('.')\n",
    "    return files\n",
    "\n",
    "def find_longest_file():\n",
    "    files = list_files()\n",
    "    longest_file = max(files, key=len)\n",
    "    return longest_file\n",
    "\n",
    "def group_files_by_type():\n",
    "    files = list_files()\n",
    "    file_dict = {}\n",
    "    for file in files:\n",
    "        file_type = os.path.splitext(file)[1]\n",
    "        if file_type not in file_dict:\n",
    "            file_dict[file_type] = []\n",
    "        file_dict[file_type].append(file)\n",
    "    return file_dict\n",
    "\n",
    "# To use these functions, you can call them like this:\n",
    "print(list_files())\n",
    "print(find_longest_file())\n",
    "print(group_files_by_type())\n",
    "# In this code, `list_files` function lists all the files in the current directory, `find_longest_file` function finds the file with the longest name in the current directory, and `group_files_by_type` function generates a dictionary that groups the files in the current folder based on their types."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "689298e8-7070-43c1-927a-a5521b2ee08d",
   "metadata": {},
   "source": [
    "### `%%chat -s` or `%%chat --safe` will invoke safe mode that won't execute the cells "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f353789b-c8e7-467c-bce6-1ed02eb33fe9",
   "metadata": {
    "ChapyterCell": {
     "cellType": "original",
     "linkedCellId": "54c09d95-8df0-41b5-89a2-28475f7d0c47"
    }
   },
   "outputs": [],
   "source": [
    "%%chat -s \n",
    "\n",
    "Generate a dictionary that groups the files in the current folder based on their types"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "54c09d95-8df0-41b5-89a2-28475f7d0c47",
   "metadata": {
    "ChapyterCell": {
     "cellType": "generated",
     "linkedCellId": "f353789b-c8e7-467c-bce6-1ed02eb33fe9"
    }
   },
   "outputs": [],
   "source": [
    "# Assistant Code for Cell [6]:\n",
    "# Sure, here is a Python script that does that:\n",
    "import os\n",
    "\n",
    "# Get all files in current directory\n",
    "files = os.listdir('.')\n",
    "\n",
    "# Initialize an empty dictionary to store the file types and names\n",
    "file_dict = {}\n",
    "\n",
    "# Loop through each file\n",
    "for file in files:\n",
    "    # Get the file extension\n",
    "    file_type = os.path.splitext(file)[1]\n",
    "    \n",
    "    # If the file type is not in the dictionary, add it\n",
    "    if file_type not in file_dict:\n",
    "        file_dict[file_type] = []\n",
    "    \n",
    "    # Add the file name to the list of files for this type\n",
    "    file_dict[file_type].append(file)\n",
    "\n",
    "print(file_dict)\n",
    "# This script will print a dictionary where the keys are file extensions (like '.txt', '.py', etc.) and the values are lists of file names with that extension."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e89ac15-bf8f-4763-bab0-2dcb089e96c3",
   "metadata": {},
   "source": [
    "### `%%chatonly` allows you to talk to an agent in regular ways "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "fdebb778-26a4-4db4-9a74-9191d54516c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "In the realm of code, where logic is the language,\n",
      "There exists a serpent, not of danger, but of change.\n",
      "Python, they call it, a creature of grace,\n",
      "In the world of programming, it's found its place.\n",
      "\n",
      "Lines of elegance, simplicity its charm,\n",
      "For beginners and experts, it causes no harm.\n",
      "From data science to web design,\n",
      "Its versatility is truly divine.\n",
      "\n",
      "Indentation matters, in its syntax so neat,\n",
      "Making code readable, a feat so sweet.\n",
      "With libraries abundant, like stars in the sky,\n",
      "The solutions it offers, they never run dry.\n",
      "\n",
      "In the heart of a coder, Python ignites a flame,\n",
      "Turning problems to solutions, that's the aim.\n",
      "From machine learning to artificial intelligence,\n",
      "It breaks all barriers, knows no resistance.\n",
      "\n",
      "Python, oh Python, in bytes we converse,\n",
      "Your presence in coding universe is diverse.\n",
      "In the dance of algorithms, you're a trusted partner,\n",
      "For many a programmer, you're the lantern.\n",
      "\n",
      "So here's to Python, a toast, a cheer,\n",
      "To a language we coders, hold so dear.\n",
      "In the world of technology, constantly in motion,\n",
      "Python flows like a smooth, steady ocean.\n"
     ]
    }
   ],
   "source": [
    "%%chatonly\n",
    "Can you write a poem for the python programming language? "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "extension",
   "language": "python",
   "name": "extension"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
